package ee.ttu.karileet.serviceshop.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import ee.ttu.karileet.serviceshop.dao.UserAccountDAO;
import ee.ttu.karileet.serviceshop.model.UserAccount;

@Service
public class UserAccountDAOImpl implements UserAccountDAO {
	
	@Autowired
	private SessionFactory sessionFactory;
	
	@Override
	public void insertUserAccount(UserAccount userAccount) {
		sessionFactory.getCurrentSession().save(userAccount);
	}

	@Override
	public UserAccount getUserAccountById(
			long userAccountId) {
		return (UserAccount) sessionFactory.
			      getCurrentSession().
			      get(UserAccount.class, userAccountId);
	}

	@Override
	public UserAccount getUserAccount(
			String username) {
		Query query = sessionFactory.
			      getCurrentSession().
			      createQuery("from UserAccount where username = :username");
			    query.setParameter("username", username);
	    return (UserAccount) query.list().get(0);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<UserAccount> getUserAccounts() {
	    Criteria criteria = sessionFactory.
	    	      getCurrentSession().
	    	      createCriteria(UserAccount.class);
	    return criteria.list();
	}

}
